#!/usr/bin/env python
# -*- encoding: UTF-8 -*-
from __future__ import print_function

import cv2
import os
from app.models.Nao import Nao
from app.models.Cam import Cam
from app.models.Image import Image

basedir = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.join(basedir,'data')

def init_db(db):
    db.session.add(Nao("red","localhost",9559))
    db.session.add(Nao("blue","localhost",9560))
    db.session.commit()
    for nao_name in ["red","blue"]:
    #for nao_name in ["red"]:
        nao = Nao.get_by_name(nao_name)
        for postion in ["bottom","top"]:
        #for postion in ["top"]:
            cam = nao.get_cam(position=postion)
            image_dir = os.path.join(data_dir,nao_name,postion)
            for image_name in os.listdir(image_dir):
                assert image_name.endswith(".jpg")
                name = image_name.split('_')[0]
                image = Image(cam,name)
                image = image.commit()
                image.save(cv2.imread(os.path.join(image_dir,image_name)))
                db.session.add(image)
            db.session.add(cam)
    db.session.commit()

